package tools.hhsearch;

import java.io.BufferedReader;

public class hhsParser {

	private hhsAlignment cur, next;
	private String curQuery,s;
	private boolean inHeader,hasMore;
	private BufferedReader in;
	
	public hhsParser(BufferedReader in)throws Exception{
		this.in=in;
		inHeader=true;
		hasMore=false;
		readNext();
	}
	public hhsAlignment nextAlignment()throws Exception{
		if(!hasMore){
			throw new Exception("No more alignments");
		}
		cur=new hhsAlignment(next);
		readNext();
		return cur;
	}
	public hhsAlignment getCur(){
		return cur;
	}
	public boolean hasMore(){
		return hasMore;
	}
	
	private void readNext()throws Exception{
		s=in.readLine();
		if(s==null){
			hasMore=false;
		}else{
			if(inHeader){
				for(;s!=null&&!s.startsWith(" No Hit");s=in.readLine()){
					if(s.startsWith("Query")){
						curQuery=s.substring(14).trim();
					}
				}
				inHeader=false;
				readNext();
			}else{
				try{
					next=new hhsAlignment(curQuery,s);
					hasMore=true;
				}catch(Exception e){
//					System.out.println(s);
//					e.printStackTrace();
					inHeader=true;
					readNext();
				}
			}
		}
	}
}
